﻿namespace UnitTests
{
    using NUnit.Framework;
    using Domains;
    using System.IO;
    using System;

    [TestFixture]
    public class TestComparedFiles
    {
        [Test]
        public void TestAddEmptyLines()
        {
            string LeftFile = Execution.FileToString(DirectoryPath + "FilesExamples\\Compare\\leftCustomerPrevius.html");
            string RightFile = Execution.FileToString(DirectoryPath + "FilesExamples\\Compare\\leftCustomerNext.html");
            Execution.StringToFile(ComparedFiles.AddEmptyLines(LeftFile, RightFile), AppDomain.CurrentDomain.BaseDirectory + @"temp.html");
            StreamReader Actual = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"temp.html");
            StreamReader Expected = new StreamReader(DirectoryPath + @"FilesExamples\Merge\CustomerEmptyLines.html");
            Assert.AreEqual(Expected.BaseStream, Actual.BaseStream);
        }

        [Test]
        public void TestSelectionGluing()
        {
            string LeftHtml = "<span class=\"TextSegSigDiff\">S</span>IMPLE_ECO_7";
            string RightHtml = "SIMPLE_ECO<span class=\"TextSegSigDiff\">_7</span>";
            string InnerHtml = "SIMPLE_ECO_7";

            string Expected = ComparedFiles.SelectionGluing(LeftHtml, RightHtml, InnerHtml);
            string Actual = "<span class=\"TextSegSigDiff\">S</span>IMPLE_ECO<span class=\"TextSegSigDiff\">_7</span>";

            Assert.AreEqual(Actual, Expected);
        }

        [Test]
        public void TestMarkString()
        {
            int[] Actual = ComparedFiles.MarkString("SIMPL<span class=\"TextSegSigDiff\">E</span>_ECO<span class=\"TextSegSigDiff\">_7</span>");
            int[] Expected = new int[12] { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1 };

            Assert.AreEqual(Actual, Expected);
        }

        [Test]
        public void TestMergeMarkStrings()
        {
            int[] leftMarks = new int[12] { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 };
            int[] rightMarks = new int[12] { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1 };

            int[] Actual = ComparedFiles.MergeMarkStrings(leftMarks, rightMarks);
            int[] Expected = new int[12] { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1 };

            Assert.AreEqual(Actual, Expected);
        }

        [Test]
        public void TestDecorMarkStrings()
        {
            int[] Marks = new int[12] { 1, 0, 0, 0, 0, 1, 3, 0, 0, 0, 2, 2 };
            string HtmlString = "SIMPLE_ECO_7";

            string Actual = ComparedFiles.DecorMarkStrings(Marks, HtmlString);
            string Expected = "<span class=\"TextSegSigDiff\">S</span>IMPL<span class=\"TextSegSigDiff\">E</span><span class=\"TextSegSigDiff\">_</span>ECO<span class=\"TextSegSigDiff\">_7</span>";

            Assert.AreEqual(Expected, Actual);
        }

        /// <summary>
        /// Path to up directory which contain the project
        /// </summary>
        public static string DirectoryPath
        {
            get
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                string[] splitingpath = path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);

                string directoryPath = default(string);
                for (int i = splitingpath.Length - 4; i > -1; i--)
                {
                    directoryPath = splitingpath[i] + '\\' + directoryPath;
                }
                return directoryPath;
            }
        }
    }
}
